import { Config, PixelStreaming } from '@epicgames-ps/lib-pixelstreamingfrontend-ue5.2'
import { Application, PixelStreamingApplicationStyle, UIElementCreationMode } from '@epicgames-ps/lib-pixelstreamingfrontend-ui-ue5.2'
import './peer-stream.js'
import { PeerStream } from './peer-stream'
type PeerStream = typeof PeerStream
let stream: PeerStream | PixelStreaming | null = null
let linkType:'initLocalUePixelStreaming' | 'initUePixelStreaming' | '' = ''   // 连接类型:本地/像素包
const setStream = (inStream: PeerStream | PixelStreaming) => {
    stream = inStream
}

export const getStream = () => {
    return stream as PeerStream
}

export const initLocalUePixelStreaming = (url:string, dom:HTMLElement) => {
    linkType = 'initLocalUePixelStreaming'
    const PixelStreamingApplicationStyles = new PixelStreamingApplicationStyle()
    PixelStreamingApplicationStyles.applyStyleSheet()
    const config = new Config({ useUrlParams: true, })
    config.setTextSetting('ss',url)// ue像素流送路径
    config.setFlagEnabled('MatchViewportRes', true)// 分辨率自适应
    config.setFlagEnabled('SuppressBrowserKeys', false) // 禁用功能快捷键
    config.setFlagEnabled('HoveringMouse', true)
    const stream = new PixelStreaming(config)
    const application = new Application({
        stream: stream,
        onColorModeChanged: (isLightMode) => PixelStreamingApplicationStyles.setColorMode(isLightMode),
        settingsPanelConfig: {// 设置
            isEnabled : true,
            visibilityButtonConfig : { creationMode : UIElementCreationMode.Disable }
        },
        fullScreenControlsConfig: {// 全屏
            creationMode : UIElementCreationMode.Disable
        },
        statsPanelConfig: {// 说明
            isEnabled : true,
            visibilityButtonConfig : { creationMode : UIElementCreationMode.Disable }
        },
    })
    setStream(stream)
    dom.appendChild(application.rootElement);
    return stream
}

export const initUePixelStreaming = (url:string,dom:HTMLElement) => {
    linkType = 'initUePixelStreaming'
    const stream = document.createElement("video", { is: "peer-stream" });
    stream.id = url;
    stream.style.width = dom.style.width
    stream.style.height = dom.style.height
    setStream(stream)
    dom.appendChild(stream);
    return stream
}

export const addEventListener = (streams: PeerStream) =>{
    if(linkType === 'initLocalUePixelStreaming'){   // 本地服务
        return new Promise(function (resolve) {
            streams.addEventListener('videoEncoderAvgQP', function() {
                resolve('success')
            });
        });
    }else{
        return new Promise(function (resolve) {
            streams.addEventListener("loadeddata", (e: any) => {
                console.log(e)
                resolve('success')
            });
        })
    }
}
export const removeEventListener = (streams: PeerStream) =>{
    if(linkType === 'initLocalUePixelStreaming'){   // 本地服务
        streams.removeEventListener('videoEncoderAvgQP', function() {
        });
    }
}